
package Servlets;

import DAO.DAO_Usuario;
import Interfaces.Inter_Usuario;
import Model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




public class ServletUsuario extends HttpServlet
{

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException , Exception
    {
        Inter_Usuario dao = new DAO_Usuario();
        Usuario usuario = new Usuario();
        
         String cmd =request.getParameter("cmd");
         
          if(cmd==null)
          {
            cmd ="erro";
          }
          
          if(cmd!=null && !cmd.equals("erro"))
          {
             try{
                 
              usuario.setNOM_Usuario(request.getParameter("txtnome").toString());
              usuario.setFK_Nivel(1);
              
               }catch(Exception e){
                throw new Exception("Erro em USUARIO classe ServletUsuario \n"+e.getMessage());
               }
          }
          
          try{
              
          RequestDispatcher rd =null;

          if(cmd.equals("listar")) //Mostrar usuarios
          {
              List usuarioList = dao.ListaUsuarios();
              request.setAttribute("usuarioList",usuarioList);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("gravar"))
          {
              dao.GravaUsuarios(usuario);
              rd =request.getRequestDispatcher("/ServletUsuario?cmd=listar");
          }

          else if(cmd.equals("excluir"))
          {
              dao.ExcluiUsuarios(usuario);
              rd =request.getRequestDispatcher("/ServletUsuario?cmd=listar");
          }

          else if(cmd.equals("alterar"))
          {
              usuario =dao.LocalizaUsuarios(usuario.getID_Usuario());
              HttpSession session = request.getSession(true);
              session.setAttribute("usuario",usuario);
              rd =request.getRequestDispatcher("");
          }

          else if(cmd.equals("atualizar"))
          {
              dao.AlteraUsuarios(usuario);
              rd =request.getRequestDispatcher("/ServletUsuario?cmd =listar");
          }
          else if( cmd.equalsIgnoreCase( "principal" ) )
          {
                rd = request.getRequestDispatcher( "/index.jsp" );
          }
            rd.forward(request, response);
      }
      catch(Exception e)
        {
            e.printStackTrace( );
            throw new ServletException( e );
        }
    
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)    
      throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
     
     
     
}//fecha a classe ServletUsuario
    
   
     
  



